#include "SDLAnimation.h"


SDLAnimation::SDLAnimation()
	: SDLSprite(0)
	, currentTime(0)
	, currentFrame(0)
	, frameRate(0)
	, nbFrames(0)
	, isLooping(false)
	, isHorizontal(false)
	, frameWidth(0)
	, frameHeight(0)
{
}

SDLAnimation::SDLAnimation(int nbFrames, int frameRate, int frameX, int frameY, int width, int height, bool isLooping, bool isHorizontal, std::string fileName)
	: SDLSprite(frameX, frameY, width, height, fileName)
	, currentTime(0)
	, currentFrame(0)
	, frameRate(1.0/frameRate)
	, nbFrames(nbFrames)
	, isLooping(isLooping)
	, isHorizontal(isHorizontal)
	, frameWidth(width)
	, frameHeight(height)
{
	anim = new SDL_Rect[nbFrames];
	for(int i = 0; i < nbFrames; i++)
	{
		if(isHorizontal)
		{
			anim[i].x = frameX + i * width;
			anim[i].y = frameY;
		}
		else
		{
			anim[i].x = frameX ;
			anim[i].y = frameY + i * height;
		}
		anim[i].w = width;
		anim[i].h = height;
	}

	SetFrame(0,0,anim[currentFrame].x, anim[currentFrame].y, frameWidth, frameHeight);
}

SDLAnimation::SDLAnimation(int nbFrames, int frameRate, int posX, int posY, int frameX, int frameY, int width, int height, bool isLooping, bool isHorizontal, std::string fileName)
	: SDLSprite(posX, posY, frameX, frameY, width, height, fileName)
	, currentTime(0)
	, currentFrame(0)
	, frameRate(1.0/frameRate)
	, nbFrames(nbFrames)
	, isLooping(isLooping)
	, isHorizontal(isHorizontal)
	, frameWidth(width)
	, frameHeight(height)
{
	anim = new SDL_Rect[nbFrames];
	for(int i = 0; i < nbFrames; i++)
	{
		if(isHorizontal)
		{
			anim[i].x = frameX + i * width;
			anim[i].y = frameY;
		}
		else
		{
			anim[i].x = frameX ;
			anim[i].y = frameY + i * height;
		}
		anim[i].w = width;
		anim[i].h = height;
	}

	SetFrame(posX, posY, anim[currentFrame].x, anim[currentFrame].y, frameWidth, frameHeight);
}


SDLAnimation::SDLAnimation(int nbFrames, int frameRate, int posX, int posY, int frameX, int frameY, int width, int height, bool isLooping, bool isHorizontal, std::string fileName, SDLScene* _scene)
	: SDLSprite(posX, posY, frameX, frameY, width, height, fileName, _scene)
	, currentTime(0)
	, currentFrame(0)
	, frameRate(1.0/frameRate)
	, nbFrames(nbFrames)
	, isLooping(isLooping)
	, isHorizontal(isHorizontal)
	, frameWidth(width)
	, frameHeight(height)
{
	anim = new SDL_Rect[nbFrames];
	for(int i = 0; i < nbFrames; i++)
	{
		if(isHorizontal)
		{
			anim[i].x = frameX + i * width;
			anim[i].y = frameY;
		}
		else
		{
			anim[i].x = frameX ;
			anim[i].y = frameY + i * height;
		}
		anim[i].w = width;
		anim[i].h = height;
	}

	SetFrame(posX, posY, anim[currentFrame].x, anim[currentFrame].y, frameWidth, frameHeight);
}


SDLAnimation::~SDLAnimation()
{
	delete [] anim;
}

void SDLAnimation::SetAnimation(int _nbFrames, int _frameRate, int _posX, int _posY, int _frameX, int _frameY, int _width, int _height, bool _isLooping, bool _isHorizontal)
{
	currentFrame = 0;
	currentTime = 0;
	nbFrames = _nbFrames;
	frameRate = 1.0f/_frameRate;
	frameWidth = _width;
	frameHeight = _height;
	isLooping = _isLooping;
	isHorizontal = _isHorizontal;
	
	delete [] anim;

	anim = new SDL_Rect[nbFrames];
	for(int i = 0; i < nbFrames; i++)
	{
		if(isHorizontal)
		{
			anim[i].x = _frameX + i * _width;
			anim[i].y = _frameY;
		}
		else
		{
			anim[i].x = _frameX ;
			anim[i].y = _frameY + i * _height;
		}
		anim[i].w = _width;
		anim[i].h = _height;
	}

	SetFrame(m_PosX, m_PosY, anim[currentFrame].x, anim[currentFrame].y, frameWidth, frameHeight);
}

void SDLAnimation::Update()
{
	currentTime += SDLTime::GetInstance()->GetDeltaTime();
	if(currentTime >= frameRate)
	{
		currentTime = 0;
		if(currentFrame < nbFrames - 1)
		{
			currentFrame++;
			SetFrame(m_PosX, m_PosY, anim[currentFrame].x, anim[currentFrame].y, frameWidth, frameHeight);
		}
		else
		{
			if(isLooping)
			{
				currentFrame = 0;
				SetFrame(m_PosX, m_PosY, anim[currentFrame].x, anim[currentFrame].y, frameWidth, frameHeight);
			}
			else
			{
				SetVisible(false);
			}
		}
	}
}